﻿﻿using System;
using System.ComponentModel;
using HmFramework.Data;

/*
 * EmeCoder v1.2.1.168
 * 作者：Administrator/PC4APPLE
 * 时间：2013-04-28 02:46:30
 * 版权：版权所有 (C) Eme Development Team 2013
*/

namespace CuteSprite.Eme.Data.CRM
{
	/// <summary>快件描述</summary>
	[Serializable]
	[ModelCheckMode(ModelCheckModes.CheckTableWhenFirstUse)]
	public class ExpressDescription : ExpressDescription<ExpressDescription> { }

	/// <summary>快件描述</summary>
	public partial class ExpressDescription<TEntity> : Entity<TEntity> where TEntity : ExpressDescription<TEntity>, new()
	{
		#region 对象操作﻿

		static ExpressDescription()
		{
			// 用于引发基类的静态构造函数，所有层次的泛型实体类都应该有一个
			TEntity entity = new TEntity();
		}

		/// <summary>已重载，保存实体</summary>
		/// <returns></returns>
		public override int Save()
		{
			//var desc = FindByDescriptionCN(DescriptionCN);
			//if (desc != null)
			//{
			//	CopyFrom(desc);
			//	return ID;
			//}

			// 如果注释已经存在，直接退出
			if (!EntityHelper.IsORMRemoting && CheckDescriptionIsExist(DescriptionCN, ID)) { return 0; }

			return base.Save();
		}

		#endregion

		#region 扩展属性﻿

		#endregion

		#region 扩展查询﻿

		/// <summary>根据中文描述查找</summary>
		/// <param name="descriptioncn">中文描述</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByDescriptionCN(String descriptioncn)
		{
			if (Meta.Count > 1000)
				return Find(_.DescriptionCN, descriptioncn);
			else // 实体缓存
				return Meta.Cache.Entities.Find(_.DescriptionCN, descriptioncn);
			// 单对象缓存
			//return Meta.SingleCache[descriptioncn];
		}

		/// <summary>根据拼音缩写查找</summary>
		/// <param name="pinyincode">拼音缩写</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static EntityList<TEntity> FindAllByPinYinCode(String pinyincode)
		{
			if (Meta.Count > 1000)
				return FindAll(_.PinYinCode, pinyincode);
			else // 实体缓存
				return Meta.Cache.Entities.FindAll(_.PinYinCode, pinyincode);
		}

		/// <summary>根据主键查找</summary>
		/// <param name="id">主键</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByID(Int32 id)
		{
			if (Meta.Count > 1000)
				return Find(_.ID, id);
			else // 实体缓存
				return Meta.Cache.Entities.Find(_.ID, id);
			// 单对象缓存
			//return Meta.SingleCache[id];
		}

		#endregion

		#region 扩展操作

		#endregion

		#region 业务

		/// <summary>根据中文描述查找</summary>
		/// <param name="descriptioncn">中文描述</param>
		/// <param name="excludedID">排除实例本身ID</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static Boolean CheckDescriptionIsExist(String descriptioncn, Int32 excludedID)
		{
			TEntity entity = null;
			if (excludedID > 0)
			{
				if (Meta.Count > 1000)
				{
					entity = Find(_.DescriptionCN.Equal(descriptioncn) & _.ID.NotEqual(excludedID));
				}
				else
				{
					entity = Meta.Cache.Entities.Find(e => (e.DescriptionCN == descriptioncn && e.ID != excludedID));
				}
			}
			else
			{
				entity = FindByDescriptionCN(descriptioncn);
			}
			return entity != null;
		}

		#endregion
	}
}